﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Xml;

namespace CrazyWorship1
{

    public class Screen : IXmlSerializable, INotifyPropertyChanged
    {
        public Screen()
        {
        }
        public Screen(Guid id, string deviceName, string clientUrl, Rectangle bounds)
        {
            _deviceName = deviceName;
            _clientUrl = clientUrl;
            Bounds = bounds;
            Bounds.PropertyChanged += new PropertyChangedEventHandler(Bounds_PropertyChanged);
            _screenId = id;
            _isFullScreen = false;
        }
        public Screen(Guid id, string deviceName, string clientUrl)
        {
            _deviceName = deviceName;
            _clientUrl = clientUrl;
            _screenId = id;
            Bounds = new Rectangle(0, 0, 160, 90);
            Bounds.PropertyChanged += new PropertyChangedEventHandler(Bounds_PropertyChanged);
            _isFullScreen = true;
        }

        void Bounds_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            NotifyPropertyChanged("Bounds");
        }
        public string DeviceName
        {
            get { return _deviceName; }
            set { _deviceName = value; }
        }
        string _deviceName;
        public Rectangle Bounds
        {
            get { return _bounds; }
            set
            {
                _bounds = value;
                NotifyPropertyChanged("Bounds");
            }
        }
        Guid _screenId;
        public Guid ScreenId
        {
            get
            {
                return _screenId;
            }
            set
            {
                _screenId = value;
            }

        }
        Rectangle _bounds;
        public bool IsFullScreen
        {
            get { return _isFullScreen; }
            set
            {
                _isFullScreen = value;
                NotifyPropertyChanged("IsFullScreen");
            }
        }
        string _clientUrl;
        public string ClientUrl
        {
            get
            {
                return _clientUrl;
            }
        }
        bool _isFullScreen;

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement("Screen");
            this._deviceName = reader.ReadElementContentAsString("_deviceName", "");
            this._clientUrl = reader.ReadElementContentAsString("_clientUrl", "");
            this._screenId = new Guid((string)reader.ReadElementContentAsObject("_guid", ""));
            this._bounds = new Rectangle();//10, 10, 10, 10);
            _bounds.ReadXml(reader);
            this._isFullScreen = bool.Parse((string)reader.ReadElementContentAsObject("_isFullScreen", ""));
            reader.ReadEndElement();
            reader.MoveToContent();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("Screen");
            writer.WriteElementString("_deviceName", _deviceName);
            writer.WriteElementString("_clientUrl", _clientUrl);
            writer.WriteStartElement("_guid", "");
            writer.WriteValue(_screenId.ToString());
            writer.WriteEndElement();
            _bounds.WriteXml(writer);
            //writer.WriteElementString("_bounds", _bounds.ToString());
            writer.WriteElementString("_isFullScreen", _isFullScreen.ToString());
            writer.WriteEndElement();
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }


        #endregion
    }
}
